//
//  WithdrawViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/5/8.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "WithdrawViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "TiXianShuoMingViewController.h"
#import "YoungAllData.h"
#import "Alart_ZhiFuView.h"
#import "GMDCircleLoader.h"

@interface WithdrawViewController ()<UITextFieldDelegate,YoungDelegate,UIAlertViewDelegate>
{
    UIImageView *titleBgView;
    UILabel *name;
    UIView *views;
    UIView *alertView;
    UILabel *rightLabel ;
    Alart_ZhiFuView *_zhiFuAlartView;//提交订单时的弹出框
    UIView *_alertBackview;
    UITextField *nameTextField;
}
@end

@implementation WithdrawViewController
@synthesize keYongStr,nameStr;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    navTitle.text = @"提现";
    [self initUI];
}
#pragma mark - initUI
-(void)initUI{
    titleBgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, navView.bottom,SCREEN_WIDTH, 180)];
    titleBgView.backgroundColor = [UIColor whiteColor];
    titleBgView.userInteractionEnabled = YES;
    [self.view addSubview:titleBgView];
    
    
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 1, 70, 40)];
    label1.text = @"欢迎回来";
    label1.textColor = [UIColor blackColor];
    label1.font = [UIFont systemFontOfSize:16];
    [titleBgView addSubview:label1];
    
    name = [[UILabel alloc] initWithFrame:CGRectMake(label1.right, label1.top, 200, 40)];
    name.text = nameStr;
    name.font = [UIFont systemFontOfSize:16];
    [titleBgView addSubview:name];
    
    [self createrOffLineUI:CGRectMake(10, name.bottom, SCREEN_WIDTH-20, 0.5)];
    
    UIImageView *zImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bangdingyinhangka-xiugaixinxi"]];
    zImage.frame = CGRectMake(10, name.bottom+15, SCREEN_WIDTH-20, 30);
    [titleBgView addSubview:zImage];
    
    UILabel *leftLabel  = [[UILabel alloc]initWithFrame:CGRectMake(5, 0 ,zImage.frame.size.width/2 , 30)];
    leftLabel.text = @"可用金额:";
    leftLabel.textColor = [self colorWithHexString:@"#5D5D5D"];
    leftLabel.textAlignment = NSTextAlignmentLeft;
    leftLabel.font = [UIFont systemFontOfSize:14];
    [zImage addSubview:leftLabel];
    
    rightLabel  = [[UILabel alloc]initWithFrame:CGRectMake(zImage.frame.size.width/2, 0 ,SCREEN_WIDTH/2-15 , 30)];
    rightLabel.text = keYongStr;
    rightLabel.textColor = [self colorWithHexString:@"#EB2F6C"];
    rightLabel.textAlignment = NSTextAlignmentRight;
    rightLabel.font = [UIFont systemFontOfSize:14];
    [zImage addSubview:rightLabel];
    
    // UIImage *image = [[UIImage imageNamed:@"xxx"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
    UIImageView *tvView = [[UIImageView alloc]initWithFrame:CGRectMake(10, zImage.bottom+20, 200, 31)];
    tvView.image = [[UIImage imageNamed:@"账户---提现_06"]stretchableImageWithLeftCapWidth:10 topCapHeight:10];
    tvView.userInteractionEnabled = YES;
    [titleBgView addSubview:tvView];
    
    nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0 ,tvView.frame.size.width-20 , 31)];
    nameTextField.tag = 300;
    nameTextField.placeholder = @"输入提现金额";
    nameTextField.font = [UIFont systemFontOfSize:14];
    //writePhoneNum.backgroundColor= [UIColor whiteColor];
    [tvView addSubview:nameTextField];
    nameTextField.delegate = self;
    
    UIButton *tiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    tiBtn.frame=  CGRectMake(tvView.right+5, tvView.top, 95, 30);
    [tiBtn setBackgroundImage:[UIImage imageNamed:@"账户---提现_08"] forState:UIControlStateNormal];
    [tiBtn addTarget:self action:@selector(clickTi) forControlEvents:UIControlEventTouchUpInside];
    [titleBgView addSubview:tiBtn];
    
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, tiBtn.bottom+5, SCREEN_WIDTH-20, 40)];
    label2.text = @"提现后,提现金额将于第二天到账,到账前处于冻结状态";
    label2.textColor = [UIColor lightGrayColor];
    label2.textAlignment = NSTextAlignmentLeft;
    label2.font = [UIFont systemFontOfSize:12];
    [titleBgView addSubview:label2];
    //账户---提现_13
    
    UIButton *tixianBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    tixianBtn.frame=  CGRectMake(10, titleBgView.bottom+20, SCREEN_WIDTH-20, 27);
    [tixianBtn setBackgroundImage:[UIImage imageNamed:@"账户---提现_13"] forState:UIControlStateNormal];
    [tixianBtn addTarget:self action:@selector(clickTiShuo) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tixianBtn];
    
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [self.view addGestureRecognizer:singleTapGestureRecognizer];
    
}
-(void)singleTap{
    [nameTextField resignFirstResponder];
    [_zhiFuAlartView.password resignFirstResponder];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    textField.keyboardType = UIKeyboardTypeNumberPad;
    return YES;
}
#pragma mark - UItextField Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

//    NSMutableString *text = [textField.text mutableCopy];
//    [text replaceCharactersInRange:range withString:string];
//    return ([text length] >= 6);
    return YES;
}

#pragma mark -提现说明
-(void)clickTiShuo{
    TiXianShuoMingViewController *tixian = [[TiXianShuoMingViewController alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:tixian animated:YES];
}
#pragma mark - 提现
-(void)clickTi{
    [nameTextField resignFirstResponder];
    [self careteAlertPassword];
}
#pragma -mark 提交订单时的弹出输入密码提示框
-(void)careteAlertPassword{
    
    UIWindow *windows = [[[UIApplication sharedApplication] delegate] window];
    
    _alertBackview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _alertBackview.backgroundColor = [UIColor blackColor];
    _alertBackview.alpha = 0.4;
    [windows addSubview:_alertBackview];
    
    _zhiFuAlartView = [[[NSBundle mainBundle] loadNibNamed:@"Shopping_Cart_Xib" owner:self options:nil] objectAtIndexedSubscript:12];
    _zhiFuAlartView.frame = CGRectMake(60, SCREEN_HEIGHT/2-90, SCREEN_WIDTH-120, 140);
    _zhiFuAlartView.password.delegate = self;
    [_zhiFuAlartView.affirmBut addTarget:self action:@selector(affirmClick) forControlEvents:UIControlEventTouchUpInside];
    [_zhiFuAlartView.cancelBut addTarget:self action:@selector(cancelButClick) forControlEvents:UIControlEventTouchUpInside];
    [windows addSubview:_zhiFuAlartView];

    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [_alertBackview addGestureRecognizer:singleTapGestureRecognizer];
    
    NSString *title = [NSString stringWithFormat:@"请输入您的支付密码"];
    _zhiFuAlartView.titleAlart.text = title;
}
#pragma -mark 点击弹出框的确定交易
- (void)affirmClick{
    if (_zhiFuAlartView.password.text== nil ||[_zhiFuAlartView.password.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入支付密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    [_alertBackview removeFromSuperview];
    [_zhiFuAlartView removeFromSuperview];
    [self tiXian];
}
-(void)tiXian{
    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate = self;
    [data checkTrade:[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"] andTradePass:_zhiFuAlartView.password.text];
    [GMDCircleLoader setOnView:self.view withTitle:@"提现中···" animated:YES];
    
}
-(void)checktrade:(NSDictionary *)sign{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
    UITextField *text = (UITextField *)[self.view viewWithTag:300];
    YoungAllData *data =[[YoungAllData alloc]init];
    data.delegate = self;
    
    [data goWithdraw:[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"] andAmount:text.text andTradePass:_zhiFuAlartView.password.text];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma -mark 点击弹出框的取消交易
- (void)cancelButClick{
    [_alertBackview removeFromSuperview];
    [_zhiFuAlartView removeFromSuperview];
}


#pragma mark - 提现回调
-(void)withdraw:(NSDictionary *)sign{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
    [[NSNotificationCenter defaultCenter]postNotificationName:@"updataInfo" object:nil];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"您提现成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    alert.delegate = self;
    [alert show];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"backShua" object:nil];
    //[self withdraw];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];

    }
}
#pragma mark - 提现回调失败
-(void)requistFail{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        YoungAllData *data = [[YoungAllData alloc]init];
        data.delegate = self;
        [data gainUserInfo:[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"]];
    }
}
#pragma mark - 获取信息回调
-(void)userInfo:(NSDictionary *)sign{
    NSLog(@"%@",sign);
    NSString *AccountAmount = [[sign objectForKey:@"data"] objectForKey:@"AccountAmount"];
    NSString *FreezeAmount = [[sign objectForKey:@"data"] objectForKey:@"FreezeAmount"];
    NSInteger total = [AccountAmount integerValue];
    NSInteger freeze = [FreezeAmount integerValue];
    NSInteger keyong = total - freeze;
    rightLabel .text = [NSString stringWithFormat:@"%ld",(long)keyong];
}
#pragma mark -提示框
-(void)withdraw{
    
    views = [[UIView alloc]init];
    views.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    views.userInteractionEnabled = YES;
    views.backgroundColor = [UIColor blackColor];
    views.alpha = 0.4;
    [self.tabBarController.view addSubview:views];
    
    alertView = [[UIView alloc]initWithFrame:CGRectMake(60, SCREEN_HEIGHT/2-150, SCREEN_WIDTH-120, 120)];
    alertView.backgroundColor = [UIColor whiteColor];
    alertView.userInteractionEnabled = YES;
    alertView.layer.cornerRadius = 10;//设置那个圆角的有多圆
    [self.tabBarController.view addSubview:alertView];
    
    UILabel *alertLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 25, alertView.frame.size.width-40, 20)];
    alertLabel1.text = @"提现失败";
    alertLabel1.numberOfLines = 0;
    alertLabel1.textColor = [self colorWithHexString:@"#EB2F6C"];
    alertLabel1.font = [UIFont systemFontOfSize:14];
    alertLabel1.textAlignment = NSTextAlignmentCenter;
    [alertView addSubview:alertLabel1];
    
    
    UILabel *alertLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, alertLabel1.bottom+5, alertView.frame.size.width-40, 20)];
    alertLabel.text = @"提现金额不能大于账户金额";
    alertLabel.numberOfLines = 0;
    alertLabel.textColor = [UIColor blackColor];
    alertLabel.font = [UIFont systemFontOfSize:12];
    alertLabel.textAlignment = NSTextAlignmentCenter;
    [alertView addSubview:alertLabel];
    
    // [self createrOffLineUI:CGRectMake(0, alertLabel.bottom+31, alertView.frame.size.width, 0.5)];
    
    //
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, alertLabel.bottom+22, alertView.frame.size.width, 28);
    UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:leftBtn.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
    maskLayer1.frame = leftBtn.bounds;
    maskLayer1.path = maskPath1.CGPath;
    leftBtn.layer.mask = maskLayer1;
    
    [leftBtn addTarget:self action:@selector(cliclRight) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setImage:[UIImage imageNamed:@"账户---提现--弹窗_03"] forState:UIControlStateNormal];
    [leftBtn setImage:[UIImage imageNamed:@"账户---提现--弹窗_03"] forState:UIControlStateHighlighted];
    [alertView addSubview:leftBtn];
    
    
    
}
-(void)cliclRight{
    [alertView removeFromSuperview];
    alertView = nil;
    [views removeFromSuperview];
    views =nil;
}
#pragma mark - 返回
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}
-(void)createrOffLineUI:(CGRect)rect{
    UIView *lines = [[UIView alloc] initWithFrame:rect];
    lines.backgroundColor = [UIColor grayColor];
    [titleBgView addSubview:lines];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
